import { isLastPage } from '@/utils/dataUtils'
import { useInfiniteQuery, useQuery, useMutation } from '@tanstack/vue-query'
import {
  getSystemInfoList,
  getMessageList,
  getUnreadMessageCount,
  allReadMessage,
  readMessage,
} from '@/axios/api/message'
// 系统消息
export function useSystemInfoListQuery() {
  return useInfiniteQuery({
    queryKey: ['systemInfoList'],
    queryFn: getSystemInfoList,
    getNextPageParam: (lastPage) => {
      if (isLastPage(lastPage)) {
        return undefined
      }
      return lastPage.result.current_page + 1
    },
  })
}

// 点赞、评论、关注消息列表
export function useMessageListQuery(type) {
  return useInfiniteQuery({
    queryKey: ['messageList', type],
    queryFn: getMessageList,
    getNextPageParam: (lastPage) => {
      if (isLastPage(lastPage)) {
        return undefined
      }
      return lastPage.result.current_page + 1
    },
  })
}

// 获取未读消息数量
export function useUnreadMessageCountQuery() {
  return useQuery({
    queryKey: ['unreadMessageCount'],
    queryFn: getUnreadMessageCount,
  })
}

// 一键已读
export function useAllReadMessageQuery() {
  return useMutation({
    mutationFn: allReadMessage,
  })
}

// 读取消息
export function useReadMessageQuery() {
  return useMutation({
    mutationFn: readMessage,
  })
}
